import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import React from "react";
import classNames from "classnames";
import CpasIcon from "../../components/Icon/CpasIcon";
export function InputTextAreaLengthInfo(WrappedComponent) {
    return class extends WrappedComponent {
        len = 0;
        current = 0;
        constructor(...args) {
            super(...args);
            this.state = { value: "" };
        }
        onChangeHandler = (event) => {
            const { onChange } = this.props;
            const value = event.target.value;
            this.current = value.length;
            this.setState({
                value: value,
            }, () => {
                if (typeof onChange === "function") {
                    onChange(event);
                }
            });
        };
        render() {
            const parent = super.render();
            this.len = this.props.maxLength;
            let props = {
                ...this.props,
                className: "sr-input-textarea",
                onChange: this.onChangeHandler,
                value: this.state.value,
            };
            return (_jsxs("div", { className: "sr-input-textarea-content", children: [React.cloneElement(parent, { ...props }), _jsxs("span", { className: "sr-input-textarea-count", children: [this.current, "/", this.len] })] }));
        }
    };
}
export function InputStyle(WrappedComponent) {
    // 泛型限定
    return class extends WrappedComponent {
        setClass = {};
        blurTask = () => {
            this.setClass["number-picker-active"] = false;
            this.forceUpdate();
        };
        blurHandler = () => {
            this.blurTask();
        };
        focusHandler = () => {
            this.setClass["number-picker-active"] = true;
            this.forceUpdate();
        };
        render() {
            const { sz, prefix, suffix, className } = this.props;
            this.setClass[`sr-ant-input-${sz}`] = sz;
            const parent = super.render();
            const klass = classNames("sr-ant-input", className, this.setClass);
            let props = {
                ...this.props,
                className: klass,
                onFocus: this.focusHandler,
                onBlur: this.blurHandler,
            };
            let s = {
                suffix,
                prefix,
            };
            new Map(Object.entries(s)).forEach((item, index) => {
                if (item && !item.width) {
                    props[index] = React.cloneElement(item, { width: 20, height: 20 });
                }
            });
            return React.cloneElement(parent, { ...props });
        }
    };
}
export function InputNumber(WrappedComponent) {
    // 泛型限定
    return class extends WrappedComponent {
        state = {
            value: this.props.value,
            prevPropsValue: this.props.value,
        };
        constructor(...args) {
            super(...args);
            this.onChangeHandler = this.onChangeHandler.bind(this);
        }
        static getDerivedStateFromProps(props, state) {
            if (props.value !== state.prevPropsValue) {
                return {
                    value: props.value,
                    prevPropsValue: props.value,
                };
            }
            else {
                return null;
            }
        }
        blurHandler = (event) => {
            this.blurTask();
            let value = event.target.value;
            const { max, min } = this.props;
            if (value > max && max) {
                value = max;
            }
            if (value < min && min) {
                value = min;
            }
            this.setValue(value);
        };
        filterNumber(value) {
            return this.baseFilterNumber(value);
        }
        baseFilterNumber(value) {
            const number = parseFloat(value.replace(/[^\d]/g, ""));
            return isNaN(number) ? "" : number;
        }
        setValue(value) {
            const { onChange } = this.props;
            this.setState((state) => {
                return {
                    ...state,
                    value: value,
                };
            });
            if (typeof onChange === "function") {
                onChange(value);
            }
        }
        onChangeHandler = (event) => {
            this.setValue(this.filterNumber(event.target.value));
        };
        render() {
            let props = {
                ...this.props,
                onChange: this.onChangeHandler,
                value: this.state.value,
                onBlur: this.blurHandler,
            };
            const parent = super.render();
            return React.cloneElement(parent, { ...props });
        }
    };
}
export function InputNumberLone(WrappedComponent) {
    // 泛型限定
    return class extends WrappedComponent {
        longFilterNumber(value) {
            const val = this.baseFilterNumber(value) + "";
            let a_val = [];
            let j = 1;
            for (let i = 0; i < val.length; i++) {
                if (i < 3) {
                    a_val[0] = val.substring(0, i + 1);
                }
                else {
                    if (i !== 3 && (i + 1) % 4 === 0) {
                        j++;
                    }
                    if (j === 1) {
                        a_val[j] = val.substring(3, i + 1);
                    }
                    else {
                        a_val[j] = val.substring(3 + (j - 1) * 4, i + 1);
                    }
                }
            }
            return a_val.join(" ");
        }
        filterNumber(value) {
            return this.longFilterNumber(value);
        }
        render() {
            const props = {
                ...this.props,
                onChange: this.onChangeHandler,
            };
            const parent = super.render();
            return React.cloneElement(parent, {
                ...props,
            });
        }
    };
}
export function InputNumberMoney(WrappedComponent) {
    // 泛型限定
    return class extends WrappedComponent {
        unit = "";
        filterNumber(value) {
            let val = this.baseFilterNumber(value);
            const unitName = [
                // `${intl("moneyUnit", "万")}`,
                "万",
                "十万",
                "百万",
                "千万",
            ];
            if (val > 9999) {
                const countUnit = val.toString().length - 5;
                if (countUnit >= 0) {
                    this.unit = unitName[countUnit];
                }
            }
            else {
                this.unit = "";
            }
            if (val > 99999999) {
                val = 99999999;
            }
            return val;
        }
        render() {
            let props = {
                ...this.props,
                onChange: this.onChangeHandler,
            };
            const parent = super.render();
            return (_jsxs("div", { className: "input-number-money", children: [React.cloneElement(parent, { ...props }), _jsx("div", { className: "unit", children: this.unit })] }));
        }
    };
}
export function InputNumberPicker(WrappedComponent) {
    // 泛型限定
    return class extends WrappedComponent {
        state = {
            value: this.props.value || 0,
            prevPropsValue: this.props.value,
        };
        render() {
            let props = {
                ...this.props,
                onChange: this.onChangeHandler,
            };
            const parent = super.render();
            const klass = classNames("input-number-picker", {
                "input-number-picker-disabled": this.props.disabled,
            });
            const add = () => {
                this.setState((state) => {
                    return { value: parseInt(state.value) + 1 };
                });
            };
            const im = () => {
                this.setState((state) => {
                    return { value: parseInt(state.value) - 1 };
                });
            };
            return React.cloneElement(parent, {
                ...props,
                className: klass,
                addonAfter: (_jsx("div", { onClick: add, className: "svg-content", children: _jsx(CpasIcon, { type: "iconlplus" }) })),
                addonBefore: (_jsx("div", { onClick: im, className: "svg-content", children: _jsx(CpasIcon, { type: "iconlminus" }) })),
            });
        }
    };
}
